<template>
	<jk-dialog
		v-model="showDialog"
		title="告警详情"
		width="800px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		:show-confirm-btn="false"
		:show-close-btn="true"
		:show-cancel-btn="false"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<div ref="box1">
				<el-tag type="primary" effect="plain" class="margin-bottom-4">
					<span class="el-icon-tickets item-gap"></span>
					<span>基本信息</span>
				</el-tag>
				<jk-card>
					<el-row>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="告警类型">
								<span v-if="ruleForm.alarmType = 1" class="read-only-2">工资推送</span>
								<span v-else-if="ruleForm.alarmType = 2" class="read-only-2">开台提醒</span>
								<span v-else-if="ruleForm.alarmType = 3" class="read-only-2">温度告警</span>
								<span v-else-if="ruleForm.alarmType = 4" class="read-only-2">湿度告警</span>
								<span v-else-if="ruleForm.alarmType = 5" class="read-only-2">停机提醒</span>
								<span v-else-if="ruleForm.alarmType = 6" class="read-only-2">产量预警</span>
								<span v-else class="read-only-2">落纱预警</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="告警时间">
								<span class="read-only-2">{{ ruleForm.alarmTime }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="告警对象">
								<span class="read-only-2">{{ ruleForm.alarmObject }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="告警等级">
								<span v-if="ruleForm.alarmGrade = 0" class="read-only-2">高</span>
								<span v-else-if="ruleForm.alarmGrade = 1" class="read-only-2">中</span>
								<span v-else class="read-only-2">低</span>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item class="margin-bottom-10" label="告警内容">
								<span class="read-only-2">{{ ruleForm.alarmContent }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item class="margin-bottom-10" label="触发条件说明">
								<span class="read-only-2">{{ ruleForm.triggerRemark }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-10" label="通知方式">
								<span v-if="ruleForm.notificationMethod = 0" class="read-only-2">系统推送</span>
								<span v-else class="read-only-2">公众号推送</span>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item class="margin-bottom-0" label="关联人员">
								<span class="read-only-2">{{ ruleForm.peopleNumber }}人</span>
							</el-form-item>
						</el-col>
					</el-row>
				</jk-card>
				<el-tag type="primary" effect="plain" class="margin-bottom-4 margin-top-10">
					<span class="el-icon-tickets item-gap"></span>
					<span>处理信息</span>
				</el-tag>
				<jk-card>
					<el-row>
						<el-col :span="24">
							<el-form-item class="margin-bottom-10" label="处理说明">
								<!-- <span class="read-only-2">问题已解决 serviceState</span> -->
								<span v-if="ruleForm.serviceState = 0" class="read-only-2">待处理</span>
								<span v-else-if="ruleForm.serviceState = 1" class="read-only-2">已忽略</span>
								<span v-else class="read-only-2">已处理</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="处理时间">
								<span class="read-only-2">{{ ruleForm.updateTime }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item class="margin-bottom-0" label="处理人员">
								<span class="read-only-2">{{ ruleForm.updaterName }}</span>
							</el-form-item>
						</el-col>
					</el-row>

				</jk-card>

			</div>
		</el-form>
		<checkbox-emp-dialog
			:default-select-rows="ruleForm.empList"
			check-key-name="empId"
			:dialog-state="checkboxEmpDialogState"
			@on-visible-change="onVisibleChangeOfCheckboxEmpDialog"
			@on-confirm="onConfirmBtnOfCheckboxEmpDialog"
		></checkbox-emp-dialog>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import {
        spinNotificationMessageConfigCreate,
        spinNotificationMessageConfigUpdate,
        spinNotificationMessageConfigGet, spinNotificationMessageConfigGetType
    } from '../../../api/production/wxPushConfig';
    import checkboxEmpDialog from '../../personnel/group/checkbox-emp-dialog';
    import { getAlarmMessage } from '@/api/alarm/mineWarningMessage.js';
    export default {
        components: {
            checkboxEmpDialog
        },
        props: {
            // 页面权限的前缀
            permissionPrefix: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                typeList: [
                    { id: 1, name: '工资推送' },
                    { id: 2, name: '开台提醒', description: ' (根据计划的开台时间提前提醒)' },
                    { id: 3, name: '温湿度告警', description: ' (温湿度超出设定值)' },
                    { id: 4, name: '停机提醒', description: ' (翻改停机:有翻改计划;保养停机:无生产任务+有保养任务;落纱停机:有生产任务,且在一定时间内停机的;其他停机:以上条件之外的停机)' },
                    { id: 5, name: '产量预警', description: ' (完工数量+当班产量=达到订单量的预警线时)' },
                    { id: 6, name: '落纱预警', description: ' (当前长度达到设定长度的一定比例时)' }
                ],
                showDialog: false,
                showLoading: true,
                saveBtnLoading: false,
                ruleForm: {
                    id: null,
                    messageTypeId: null,
                    messageTypeName: null,
                    remark: '',
                    roleList: [],
                    empList: [],
                    auditState: 1
                },
                rules: {
                    messageTypeId: [{ required: true, trigger: 'change' }]
                },
                checkboxEmpDialogState: false
            };
        },
        methods: {
            onDeleteBtn(idx) {
                this.ruleForm.empList.splice(idx, 1);
            },
            onVisibleChangeOfCheckboxEmpDialog(e) {
                this.checkboxEmpDialogState = e;
            },
            onConfirmBtnOfCheckboxEmpDialog(arr) {
                arr.forEach(x => {
                    this.ruleForm.empList.push({
                        empId: x.id,
                        empCode: x.code,
                        empName: x.name,
                        openId: x.openId
                    });
                });
                this.checkboxEmpDialogState = false;
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                // 新增
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    const typeItem = this.typeList.find(x => x.messageTypeId === this.ruleForm.messageTypeId) || {};
                    this.ruleForm.messageTypeName = typeItem.messageTypeName;
                    spinNotificationMessageConfigCreate(this.ruleForm).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'save');
                            this.$emit('on-finish');
                        }
                    });
                    return;
                }

                // 编辑
                spinNotificationMessageConfigUpdate(this.ruleForm).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getNoticeTypeListRequest() {
                return spinNotificationMessageConfigGetType().then(res => {
                    if (res.data.status === 200) {
                        this.typeList = res.data.res;
                    }
                });
            },
            getDetailRequest() {
                return getAlarmMessage({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                        console.log('qqqqqqqqqq', this.ruleForm);
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getNoticeTypeListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
    .wx-notice-emp-box {
        border: solid 1px #dcdee2;
        border-radius: 4px;
        overflow: auto;
        padding: 8px 10px;
        display: flex;
        justify-content: flex-start;
        align-content: flex-start;
        flex-wrap: wrap;
        box-sizing: border-box;
        .wx-notice-emp-item {
            width: calc(100% / 5);
            padding: 2px;
            flex-shrink: 0;
            box-sizing: border-box;
        }
    }
    .legend-icon-label {
        font-size: 12px;
    }
    .legend-icon {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        margin-right: 4px;
    }
    .legend-icon-primary {
        background: #2d8cf0;
    }
    .legend-icon-info {
        background: #c5c8ce;
    }
</style>
